package com.ea.eadp.nexus.test.pageDefinition.originX;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;

import com.ea.eadp.nexus.test.common.PageOperation;
import com.ea.eadp.nexus.test.pageDefinition.BasePage;

public class AgeUpPage extends BasePage {

    private static final String TITLE_ID = "underageUpgrade";
    private static final String NEXT_BUTTON_ID = "updateBtn";
    private static final String EMAIL_TEXTBOX_ID = "email";
    private static final String FISRTNAME_ID = "firstName";
    private static final String LASTNAME_ID = "lastName";
    private static final String MISS_EMAIL_ID = "form-error-missing-email";
    private static final String MISS_EMAIL_MESSAGE = "Missing email";
    private static final String INVALID_EMAIL_ID = "form-error-invalid-email";
    private static final String INVALID_EMAIL_MESSAGE = "Email address is invalid";
    private static final String DUPLICATED_EMAIL_ID = "form-error-duplicate-email";
    private static final String DUPLICATED_EMAIL_MESSAGE = "address has already been registered";

    private static final String TOS_PATH = "//div[@id='read-accept-container']/span[1]/label";
    private static final String TOS_ERROR_ID = "form-error-missing-readAccept";
    private static final String TOS_ERROR_MESSAGE = "You must accept the Privacy Policy and Terms of Service to continue";

    public AgeUpPage(final WebDriver driver) {
        super(driver, UnderageEmailVerificationPage.class);
    }

    public void waitPageLoadCompleted() {
        PageOperation.WebDriverWait(driver, TITLE_ID);
    }

    public void inputEmail(final String email) {
        PageOperation.SendKeyToElementAfterWait(driver, EMAIL_TEXTBOX_ID, email);
    }

    public void inputFirstName(final String firstName) {
        PageOperation.SendKeyToElementAfterWait(driver, FISRTNAME_ID, firstName);

    }

    public void inputLastName(final String lastName) {
        PageOperation.SendKeyToElementAfterWait(driver, LASTNAME_ID, lastName);
    }

    public void checkTOS(final Boolean checked) {
        String classes = driver.findElement(By.id("readAccept")).getAttribute("class");
        if (checked ^ classes.contains("checked")) {

            driver.findElement(By.id("readAcceptTestClickArea")).click();
        }
    }

    public void clickNextButton() {
        driver.findElement(By.id(NEXT_BUTTON_ID)).click();
    }

    public void validateMissEmail() {
        validateErrorMassage(MISS_EMAIL_ID, MISS_EMAIL_MESSAGE);
    }

    public void validateInvalidEmail() {
        validateErrorMassage(INVALID_EMAIL_ID, INVALID_EMAIL_MESSAGE);
    }

    public void validateDuplicatedEmail() {
        validateErrorMassage(DUPLICATED_EMAIL_ID, DUPLICATED_EMAIL_MESSAGE);
    }

    public void validateTOSUncheckedMessage() {
        validateErrorMassage(TOS_ERROR_ID, TOS_ERROR_MESSAGE);
    }

    public void validateSignInButtonIsDisabled() {
        Assert.assertTrue(driver.findElement(By.id(NEXT_BUTTON_ID)).getAttribute("class").contains("disabled"));
    }
}
